Skip to content

USE 113 - Rework model registration and loading#13

Merged
ghukill merged 2 commits into
mainfrom
USE-113-rework-model-registration
Oct 27, 2025
Merged

USE 113 - Rework model registration and loading#13
ghukill merged 2 commits into
mainfrom
USE-113-rework-model-registration

Conversation

@ghukill

@ghukill ghukill commented Oct 24, 2025

Copy link
Copy Markdown
Collaborator

Purpose and background context

Before diving fully into the our first model OSNeuralSparseDocV3GTE being capable of downloading the model locally, a bit of reworking for how embedding model classes are discovered and loaded.

Why these changes are being introduced:

Previously, a model's URI was only codified in the embeddings/registry.py file, and not directly obvious from a class itself aside from docstrings.

Additionally, the CLI argument --model-uri was duplicated across multiple CLI commands, where we can DRY that up and provide an environment variable default.

How this addresses that need:

Reworks the base class to require a MODEL_URI attribute. This attribute is used to generate a model_uri:class dictionary that is used for registration of models.

How can a reviewer manually see the effects of these changes?

1- Create a .env file with the following:

HF_HUB_DISABLE_PROGRESS_BARS=true
TE_MODEL_URI=opensearch-project/opensearch-neural-sparse-encoding-doc-v3-gte
TE_MODEL_DOWNLOAD_PATH=/tmp/te-model.zip

2- Run the CLI command download-model. While an error is raised because not yet implemented, note that --model-uri or --output were not required due to env vars getting set:

uv run --env-file .env embeddings download-model

NOTE: the use of uv run --env-file .env .... As noted in this uv github issue, there is lots of interest in making this default behavior... but it has not yet landed in uv. For the time being, it sounds like we may need to somewhat explicitly request that uv read the .env file when running locally.

In a deployed context, the env vars will be set and this is not required. To that end, we could also set the env vars in the terminal and just run embeddings ...; dealer's choice.

Includes new or updated dependencies?

YES

Changes expectations for external applications?

NO

What are the relevant tickets?

Code review

  • Code review best practices are documented here and you are encouraged to have a constructive dialogue with your reviewers about their preferences and expectations.

Why these changes are being introduced:

Previously, a model's URI was only codified in the registry.py file, and not
directly obvious from a class itself aside from docstrings.

How this addresses that need:

Reworks the base class to require a MODEL_URI attribute.  This attribute
is used to generate a model_uri:class dictionary that is used for registration
of models.

Side effects of this change:
* None

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/USE-113
Comment thread embeddings/models/base.py
Comment on lines +13 to +24
MODEL_URI: str # Type hint to document the requirement

def __init_subclass__(cls, **kwargs: dict) -> None: # noqa: D105
super().__init_subclass__(**kwargs)

# require class level MODEL_URI to be set
if not hasattr(cls, "MODEL_URI"):
msg = f"{cls.__name__} must define 'MODEL_URI' class attribute"
raise TypeError(msg)
if not isinstance(cls.MODEL_URI, str):
msg = f"{cls.__name__} must override 'MODEL_URI' with a valid string"
raise TypeError(msg)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This establishes MODEL_URI as a required class-level attribute for all child classes. There are different ways to achieve this, but this felt like a decent, explicit pattern here.

HuggingFace URI: opensearch-project/opensearch-neural-sparse-encoding-doc-v3-gte
"""

MODEL_URI = "opensearch-project/opensearch-neural-sparse-encoding-doc-v3-gte"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, each model (and we may only have a couple ever), will define their HuggingFace URI in the class itself.

Comment thread embeddings/cli.py
Comment on lines +16 to +29
def model_required(f: Callable) -> Callable:
"""Decorator for commands that require a specific model."""

@click.option(
"--model-uri",
envvar="TE_MODEL_URI",
required=True,
help="HuggingFace model URI (e.g., 'org/model-name')",
)
@functools.wraps(f)
def wrapper(*args: list, **kwargs: dict) -> Callable:
return f(*args, **kwargs)

return wrapper

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY's up the --model-uri CLI arg, and gives it an env var default.

@ghukill ghukill requested a review from a team October 24, 2025 20:26

@ehanson8 ehanson8 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart update and works as expected!

@ghukill ghukill merged commit 462aef5 into main Oct 27, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants